<?php

namespace app\admin\controller;

use app\admin\service\TreeService;
use app\common\model\AdminUser as AdminModel;
use app\common\model\AuthGroup as AuthGroup1;
use think\request;
use app\common\lib\Util;

class AdminUser extends Base
{

    public function index()
    {
        $userList = AdminModel::order('id', 'desc')->paginate(15);
        foreach ($userList as $key => &$value) {
            $value['avatar'] = "http://" . $_SERVER['HTTP_HOST'] . $value['avatar'];
        }
        $this->assign('title', '用户管理');
        $this->assign('user_list', $userList);
        return $this->fetch();
    }


    public function create(AdminModel $admin)
    {
        $adminData = AuthGroup1::select();
        if (request()->isPost()) {
            $rst = $admin->add(input('post.'));
            if ($rst['valid']) {
                return Util::apiResponse([], $rst['msg'], 1);
            } else {
                return Util::apiResponse([], $rst['msg'], -1);
            }
        }
        $this->assign('adminData', $adminData);
        return $this->fetch();
    }


    public function edit($id, TreeService $service, AdminModel $admin, AuthGroup1 $auth_Group)
    {
        $menuInfo = $admin->find($id);
        $auth_Group1 = $auth_Group->select();
        $menuList = $service->getMenuList();
        if (!$menuInfo) {
            $this->error('该记录不存在');
        }
        $this->assign('menu_info', $menuInfo);
        $this->assign('auth_Group1', $auth_Group1);
        $this->assign('list', $menuList);
        $this->assign('id', $id);
        return $this->fetch();
    }

    //管理员资料更新
    public function update(AdminModel $admin)
    {
        $rst = $admin->edit(input('put.'));
        if ($rst['valid']) {
            return Util::apiResponse([], $rst['msg'], 1);
        } else {
            return Util::apiResponse([], $rst['msg'], -1);
        }
    }

    //管理员删除
    public function delete()
    {
        $id = input('param.id');
        if ($id == 1) {
            return Util::apiResponse([], '你疯了吗？超级管理员都敢删除。。。', -1);

        } else {
            AdminModel::destroy($id);
            return Util::apiResponse([], '删除成功', 1);
        }
    }




    //跳转到个人资料页面
    public function profile(AdminModel $data)
    {
        $profile_data = $data->where(array('id' => session("user_session.uid")))->find();
        $profile_data['avatar'] = "http://" . $_SERVER['HTTP_HOST'] . $profile_data['avatar'];
        $this->assign('profile_data', $profile_data);
        return $this->view->fetch();
    }


    //个人资料修改
    public function profile_update(AdminModel $adminModel, Request $request)
    {
        $uploadPath = './upload/admin/headimg';
        if (!is_dir($uploadPath)) {
            mkdir($uploadPath, 0777,true);
        }
        $file = $request->file('image');
        $data = [
            'qq' => $request->qq,
            'sex' => $request->sex,
            'email' => $request->email,
            'update_time' => time(),
            'phone' => $request->phone,
            'birthday' => strtotime($request->birthday),
        ];
        if($request->password){
            $data['password'] = $request->password;
        }
        if ($file) {
            //移动文件
            $info = $file->move($uploadPath);
            if ($info) {
                // 获取上传信息
                $url = $uploadPath . $info->getSaveName();
                $data['avatar'] = $url;
            } else {
                return Util::apiResponse([], '上传图片错误！', -1);
            }
        }
        $result = $adminModel->where(array('id' => session("user_session.uid")))
            ->update($data);
        if ($result) {
            return Util::apiResponse([], '成功', 1);
        } else {
            return Util::apiResponse([], '失败', -1);
        }
    }
}
